package com.demo.dao.mapper;

import com.demo.model.Product;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class ProductMapper {
    public String insert(Product product) {
        return "insert into product (name, stock, price) values (#{name},#{stock},#{price})";
    }

    public String delete(int id) {
        BEGIN();
        DELETE_FROM("product");
        WHERE("id=#{id}");
        return SQL();
    }

    public String update(Product product) {
        BEGIN();
        UPDATE("product");
        if (product.getName() != null && product.getName().length() > 0) {
            SET("name=#{name}");
        }
        if (product.getPrice() != null && product.getPrice() > 0) {
            SET("price=#{price}");
        }
        if (product.getStock() != null && product.getStock() > 0) {
            SET("stock=#{stock}");
        }
        WHERE("id=#{id}");
        String sql = SQL();
        System.out.println(sql);
        return sql;
    }

    public String select(String name) {
        if (name == null || name.length() == 0) {
            return "select * from product order by id desc";
        } else {
            return "select * from product where name=#{name} order by id desc";
        }
    }

    public String selectOne(Integer id) {
        return "select * from product where id=#{id}";
    }
}
